$(function () {
    var rq = $.getRequest();
    let userId = getStorage("loginUserId");
    userId = userId || rq.userId || '3';
    let id = rq.id;
    let name = rq.name;
    setStorage("loginUserId", userId);
    setStorage('siteInfo', { id: id, name: name });

    $("#fsiteName1").text(name);
    $.time(function (time) {
        $("#time").html(time);
    });
    updateDate(id, name);
    updateSite(id);
    updateWarning(id, userId);
});
function updateDate(id) {
    let mapSrc = `../images/site/${id}.png`;
    api.getMainIndoorAQI(id, function (d) {
        var ps = new Array();
        d.map(function (item) {
            ps.push({
                id: item.fpointNo,
                name: item.fpointName,
                width: item.fpointW,
                heigth: item.fpointH,
                left: item.fpointD,
                top: item.fpointL
            });
        });
        $.imgMap(".imga-map", mapSrc, ps, function ($itemEl, itemData) {
            $itemEl.on('click', function () {
                var name = encodeURIComponent(itemData.name);
                location.href = `../indoor.html?id=${itemData.id}&name=${name}`;
            });
        });
    });
};

/**
 * 更新站点数据
 * @param {站点编号} id 
 */
function updateSite(id) {
    api.getOftenAirAqiByFsiteNo(id, function (d) {
        if (d.length > 0) {
            let date = d[0];
            let pm25 = [{ name: 'PM2.5', value: date.fpm25 }];
            let pm10 = [{ name: 'PM10', value: date.fpm10 }];
            let hcho = [{ name: '甲醛', value: date.fjq }];
            let tvoc = [{ name: '挥发性有机物', value: date.fyjw }];
            let co2 = [{ name: '二氧化碳', value: date.fco2 }];
            let h = [{ name: '温度', value: date.fsd2 }];
            
            let lineColor = [[0.2, '#8DC120'], [0.4, '#E8BE2C'], [0.6, '#EF8724'], [0.8, '#BF1229'], [1, '#5C1E82']];
            util.readGauge("chartpm25", pm25, { lineColor });
            util.readGauge("chartpm10", pm10, { lineColor });
            util.readGauge("charthcho", hcho, { lineColor });
            util.readGauge("charttvoc", tvoc, { lineColor });
            util.readGauge("charth", h);
            util.readGauge("chartw", co2, { lineColor });
        }
    });
}

